package org.szgb.console.sys.line.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.line.entity.SysLineLocationSystem;
import org.szgb.console.sys.line.entity.SysLineLocationSystemEquipment;
import org.szgb.console.sys.line.mapper.SysLineLocationSystemEquipmentMapper;
import org.szgb.console.sys.line.mapper.SysLineLocationSystemMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysLineLocationSystemService {
    private final SysLineLocationSystemMapper sysLineLocationSystemMapper;
    private final SysLineLocationSystemEquipmentMapper sysLineLocationSystemEquipmentMapper;

    @Autowired
    public SysLineLocationSystemService(SysLineLocationSystemMapper sysLineLocationSystemMapper,SysLineLocationSystemEquipmentMapper sysLineLocationSystemEquipmentMapper) {
        this.sysLineLocationSystemMapper = sysLineLocationSystemMapper;
        this.sysLineLocationSystemEquipmentMapper = sysLineLocationSystemEquipmentMapper;
    }

    @Transactional(readOnly = true)
    public SysLineLocationSystem get(String id) {
        return sysLineLocationSystemMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysLineLocationSystem> findList(SysLineLocationSystem sysLineLocationSystem) {
        if (sysLineLocationSystem != null && sysLineLocationSystem.getPageDto() != null) {
            Page<SysLineLocationSystem> page = new Page<>(sysLineLocationSystem.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysLineLocationSystem.setPage(page);
        }
        return sysLineLocationSystemMapper.findList(sysLineLocationSystem);
    }


    @Transactional(readOnly = true)
    public Integer getCount(SysLineLocationSystem sysLineLocationSystem) {
        return sysLineLocationSystemMapper.getCount(sysLineLocationSystem);
    }

    @Transactional()
    public void save(SysLineLocationSystem sysLineLocationSystem) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysLineLocationSystem.getId() == null || sysLineLocationSystem.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysLineLocationSystem.setCreatedBy(sysLineLocationSystem.getUpdatedBy());
            // 添加UUID
            sysLineLocationSystem.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysLineLocationSystemMapper.insert(sysLineLocationSystem);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysLineLocationSystemMapper.update(sysLineLocationSystem);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysLineLocationSystemMapper.delete(ids.split(","));
    }
}
